package com.zlhj.ex.Controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlhj.ex.Common.Res;
import com.zlhj.ex.entity.Car;
import com.zlhj.ex.entity.Info;
import com.zlhj.ex.entity.Sheng;
import com.zlhj.ex.entity.Shi;
import com.zlhj.ex.service.CarService;
import com.zlhj.ex.service.InfoService;
import com.zlhj.ex.service.ShengService;
import com.zlhj.ex.service.ShiService;
import com.zlhj.ex.vo.InfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author zsx
 * @date 2021/06/16 10:03
 */
@Api("info")
@Controller
@RequestMapping("/info")
public class InfoController {


    @Autowired
    private InfoService infoService;

    @Autowired
    private ShengService shengService;

    @Autowired
    private ShiService shiService;

    @Autowired
    private CarService carService;





    @ResponseBody
    @DeleteMapping("/deleteInfo")
    @ApiOperation(value = "删除info")
    public Res deleteInfo(){

        boolean remove = infoService.remove(null);
        if (remove){
            return new Res("sucess");
        }
        return new Res("error");

    }




    @RequestMapping("/toApplyHtml")
    public String toApplyHtml(Model model){
        List<Sheng> shengList = shengService.getShengList();
//        for (Sheng sheng : shengList) {
//            QueryWrapper<Shi> wrapper = new QueryWrapper<>();
//            wrapper.eq("sheng_id",sheng.getId());
//            List<Shi> shiList = shiService.list(wrapper);
//            sheng.setShis(shiList);
//        }
        List<Car> carList = carService.list();
        model.addAttribute("carList",carList);
        model.addAttribute("shengList",shengList);
        return "apply";
    }

//    @PostMapping("/apply")
//    @ResponseBody
//    @ApiOperation("apply")
//    public Res apply(Info info){
//        info.setCity("京A");
//        info.setMoney("100");
//        boolean save = infoService.save(info);
//        if (save){
//            return new Res("success");
//        }
//
//        return new Res("error");
//    }


    @PostMapping("/apply")
    public String apply(Info info, Model model, HttpServletRequest request){

        String p1 = request.getParameter("1");
        String p2 = request.getParameter("2");


        String uuid = IdUtil.simpleUUID().substring(0,8);
        info.setId(uuid);
        info.setCity(p1+p2);
//        info.setMoney("100");
        info.setStatus("0");
        String format = DateUtil.format(new Date(), "yyyyMMDD");
        //LocalDate now = LocalDate.now();
        //String date = DateUtil.format(new Date(), "yyyyMMdd");
//        LocalDate parse = LocalDate.parse(date);
        info.setCreateTime(format);

        boolean save = infoService.save(info);
        if (save){
            return "redirect:/info/getPageList";
//            return "redirect:/info/toApplyList";
        }
        model.addAttribute("msg","信息错误");
        return "apply";

    }



    @GetMapping("/getInfoList")
    @ResponseBody
    @ApiOperation(value = "getInfoList")
    public Res getInfoList(){
        List<Info> infoList = infoService.list();
        if (infoList.size() > 0){
            return new Res("200","success",infoList);
        } else if (infoList.size() == 0){
            return new Res("数据为空");
        }
        return new Res("error");
    }


    @ResponseBody
    @PostMapping("/getInfoListPage")
    @ApiOperation("getInfoListPage")
    public Res getInfoListPage(@RequestBody(required = false) InfoVo infoVo){

        QueryWrapper<Info> wrapper = new QueryWrapper<>();

        Page<Info> infoPage = new Page<>(1,2);

//        IPage<Info> iPage = null;
//
//        if (infoVo.getName() != null){
//            wrapper.like("name",infoVo.getName());
//
//            iPage = infoService.page(infoPage, wrapper);
//        } else {
//            iPage = infoService.page(infoPage);
//        }
//

        if (infoVo.getName() != null){
            wrapper.like("name",infoVo.getName());
        }
        if (infoVo.getCity() != null){
            wrapper.like("city",infoVo.getCity());
        }

        IPage<Info> iPage = infoService.page(infoPage, wrapper);

        long total = iPage.getTotal();
        long current = iPage.getCurrent();
        long pages = iPage.getPages();
        List<Info> records = iPage.getRecords();
        long size = iPage.getSize();

        if (records.size() > 0){
            return new Res("200","success",iPage);
        } else if (records.size() == 0){
            return new Res("success");
        }
        return new Res("error");

    }

    @RequestMapping("/toApplyList")
    public String toApplyList(Model model){
        List<Info> infoList = infoService.list();
        for (Info info : infoList) {
            if (info.getShiId() != null){
                Shi shi = shiService.getById(info.getShiId());
                model.addAttribute("shi",shi);
            }
        }
        model.addAttribute("infoList",infoList);
        return "applyList";
    }


    @RequestMapping("/toDetails")
    public String toDetails(@RequestParam(name = "id") String id,Model model){
        Info info = infoService.getById(id);
        Sheng sheng = shengService.getById(info.getShengId());
        Shi shi = shiService.getById(info.getShiId());
        model.addAttribute("info",info);
        model.addAttribute("shengName",sheng.getName());
        model.addAttribute("shiName",shi.getName());
        return "details";
    }


//    @ResponseBody
//    @PostMapping("/handle")
//    @ApiOperation(value = "受理")
//    public Res handle(@RequestParam(name = "status") String status,@RequestParam(name = "id") String id){
//
//        Info info = infoService.getById(id);
//
//        if (info != null){
//            info.setStatus(status);
//            boolean save = infoService.save(info);
//            if (save){
//                return new Res("success");
//            }
//        }
//
//        return new Res("error");
//
//    }



    @PostMapping("/handle")
    public String handle(@RequestParam(name = "id") String id,
                         @RequestParam(name = "status") String status,
                         Model model){

        Info info = infoService.getById(id);

        if (info != null){
            info.setStatus(status);
            boolean b = infoService.updateById(info);
            if (b){
                return "redirect:/info/getPageList";
            }
        }

        model.addAttribute("msg","出现错误");
        return "details";

    }



//    @PostMapping("/getInfoPageList")
//    @ResponseBody
//    @ApiOperation("查询带分页")
//    public Res getInfoPageList(InfoVo infoVo, Long currentPage, Long pageSize){
//
//        Page<Info> page = new Page<>(currentPage, pageSize);
//        QueryWrapper<Info> wrapper = new QueryWrapper<>();
//        IPage<Info> infoPage = null;
//
//        if (infoVo.getName() == null){
//            infoPage = infoService.page(page, null);
//        } else {
//            wrapper.like("name",infoVo.getName());
//            infoPage = infoService.page(page, wrapper);
//        }
//
//
//        if (infoPage.getRecords().size() > 0){
//            return new Res("200","success",infoPage);
//        } else if (infoPage.getRecords().size() == 0){
//            return new Res("数据为空");
//        }
//        return new Res("error");
//    }


//    @RequestMapping("/getInfoPageList")
    @PostMapping("/getInfoPageList")
    @ApiOperation("查询带分页")
    public String getInfoPageList(@RequestParam(name = "name")String name,
                                  @RequestParam(name = "city") String city,
                                  Model model,
                                  @RequestParam(defaultValue = "1", name = "pageNum") Long currentPage){

//        model.addAttribute("methods","post");
        model.addAttribute("name",name);



        Page<Info> page = new Page<>(currentPage, 3);
        QueryWrapper<Info> wrapper = new QueryWrapper<>();

//        IPage<Info> infoPage =null;
//
//        if (infoVo.getName() == null){
//            infoPage = infoService.page(page, null);
//        } else {
//            wrapper.like("name",infoVo.getName());
//            infoPage = infoService.page(page, wrapper);
//        }

        if (name != null){
            wrapper.like("name",name);
        }
        if (city != null){
            wrapper.like("city",city);
        }

        IPage<Info> infoPage = infoService.page(page, wrapper);

        List<Info> infoPageList = infoPage.getRecords();
        for (Info info : infoPageList) {
            Shi shi = shiService.getById(info.getShiId());
            model.addAttribute("shi",shi);
        }

        model.addAttribute("infoPageList",infoPageList);

        model.addAttribute("infoPage",infoPage);


        if (infoPage.getRecords().size() > 0){
            return "getInfoPageList";
        } else if (infoPage.getRecords().size() == 0){
            model.addAttribute("msg","数据为空");
            return "getInfoPageList";
        }
        return "e";
    }



    @GetMapping("/getPageList")
    @ApiOperation("查询带分页")
    public String getPageList(Model model,
                              @RequestParam(defaultValue = "1", name = "pageNum") long currentPage){

//        model.addAttribute("methods","get");


        QueryWrapper<Info> wrapper = new QueryWrapper<>();

        Page<Info> page = new Page<>(currentPage, 3);

        IPage<Info> infoPage =null;

//        if (name == null){
//            infoPage = infoService.page(page, null);
//        } else {
//            wrapper.like("name",name);
//            infoPage = infoService.page(page, wrapper);
//        }
        infoPage = infoService.page(page);

        List<Info> infoPageList = infoPage.getRecords();
        for (Info info : infoPageList) {
            Shi shi = shiService.getById(info.getShiId());
            model.addAttribute("shi",shi);
        }

        model.addAttribute("infoPageList",infoPageList);

        model.addAttribute("infoPage",infoPage);


        if (infoPage.getRecords().size() > 0){
            return "getInfoPageList";
        } else if (infoPage.getRecords().size() == 0){
            model.addAttribute("msg","数据为空");
            return "getInfoPageList";
        }
        return "e";
    }







}
